using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Pages_Admin_ModifyCharges : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void gvPriceTable_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (!Page.IsValid)
        {
            e.Cancel = true;
            return;
        }

        // getting the parameters to update price
        int serviceID = Int32.Parse(ddlService.SelectedValue);
        string weightDescription = gvPriceTable.Rows[gvPriceTable.EditIndex].Cells[0].Text;
        int distanceID = Int32.Parse(ddlDistance.SelectedValue);
        decimal price = decimal.Parse(e.NewValues[0].ToString());

        try {
            int affected = DALServicePrice.UpdateServicePrice(serviceID, weightDescription, distanceID, price);
            if (affected > 0)
                lblMessage.Text = "Charges updated successfully";
            else
                lblMessage.Text = "Error updating charges";
        }
        catch
        {
            lblMessage.Text = "Error updating charges";
            return;
        }

        // close the edit mode of the gridview, then refresh the gridview
        e.Cancel = true;
        gvPriceTable.EditIndex = -1;
        gvPriceTable.DataBind();
    }

    /// <summary>
    /// Validates the textbox in edit mode of gridview, the input must be a positive decimal number
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            decimal n = decimal.Parse(args.Value);
            args.IsValid = (n > 0);
        }
        catch (FormatException)
        {
            args.IsValid = false;
        }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
    }

    protected void gvPriceTable_RowEditing(object sender, GridViewEditEventArgs e)
    {
        lblMessage.Text = "";
    }
}
